Full source code giải đố hình đơn giản
16.642 lượt xem;
- SetupPuzzleGame.cs
- Puzzle Game Controller /
- Scripts /
- Assets /
- project /
1 using System.Collections;
2 using System.Collections.Generic;
3 using UnityEngine;
4 using UnityEngine.UI;
5
6
7 public class SetupPuzzleGame : MonoBehaviour {
8
9 [SerializeField]
10 private PuzzleGameManager puzzleGameManager;
11
12 private Sprite[] candyPuzzleSprites, transportPuzzleSprites, shapePuzzleSprites;
13
14 private List<Sprite> gamePuzzles = new List<Sprite>();
15
16 private List<Button> puzzleButtons = new List<Button>();
17
18 private List<Animator> puzzleButtonAnimators = new List<Animator>();
19
20 private int level;
21 private string selectedPuzzle;
22
23 private int looper;
24
25
26 void Awake()
27 {
28 candyPuzzleSprites = Resources.LoadAll<Sprite>("Art/Candies/Candy");
29 transportPuzzleSprites = Resources.LoadAll<Sprite>("Art/Transport/Transport");
30 shapePuzzleSprites = Resources.LoadAll<Sprite>("Art/Shapes/Shapes");
31
32 }
33
34 void PrepareGameSprites()
35 {
36 gamePuzzles.Clear();
37 gamePuzzles = new List<Sprite> ();
38
39 int index = 0;
40
41 switch (level)
42 {
43 case 0:
44 looper = 6;
45 break;
46
47 case 1:
48 looper = 12;
49 break;
50
51 case 2:
52 looper = 18;
53 break;
54
55 case 3:
56 looper = 24;
57 break;
58
59 case 4:
60 looper = 30;
61 break;
62 }
63
64 switch (selectedPuzzle)
65 {
66 case "Candy Puzzle":
67
68 for(int i= 0; i < looper; i++)
69 {
70 if(index == (looper / 2))
71 {
72 index = 0;
73 }
74
75 gamePuzzles.Add(candyPuzzleSprites[index]);
76
77 index++;
78 }
79 break;
80
81 case "Transport Puzzle":
82
83 for (int i = 0; i < looper; i++)
84 {
85 if (index == (looper / 2))
86 {
87 index = 0;
88 }
89
90 gamePuzzles.Add(transportPuzzleSprites[index]);
91
92 index++;
93 }
94 break;
95
96 case "Shape Puzzle":
97
98 for (int i = 0; i < looper; i++)
99 {
100 if (index == (looper / 2))
101 {
102 index = 0;
103 }
104
105 gamePuzzles.Add(shapePuzzleSprites[index]);
106
107 index++;
108 }
109 break;
110
111
112 }
113
114 Shuffle(gamePuzzles);
115 }
116
117 void Shuffle(List<Sprite> list)
118 {
119 for (int i = 0; i < list.Count; i++)
120 {
121 Sprite temp = list[i];
122 int randomIndex = Random.Range(i, list.Count);
123 list[i] = list[randomIndex];
124 list[randomIndex] = temp;
125
126 }
127 }
128
129 public void SetLevelAndPuzzle(int level, string selectedPuzzle)
130 {
131 this.level = level;
132 this.selectedPuzzle = selectedPuzzle;
133
134 PrepareGameSprites();
135
136 puzzleGameManager.SetGamePuzzleSprites(this.gamePuzzles);
137 }
138
139 public void SetPuzzleButtonsAndAnimators(List<Button> puzzleButtons, List<Animator> puzzleButtonAnimators)
140 {
141 this.puzzleButtons = puzzleButtons;
142 this.puzzleButtonAnimators = puzzleButtonAnimators;
143
144 puzzleGameManager.SetUpButtonsAndAnimators(puzzleButtons, puzzleButtonAnimators);
145 }
146 }
2 using System.Collections.Generic;
3 using UnityEngine;
4 using UnityEngine.UI;
5
6
7 public class SetupPuzzleGame : MonoBehaviour {
8
9 [SerializeField]
10 private PuzzleGameManager puzzleGameManager;
11
12 private Sprite[] candyPuzzleSprites, transportPuzzleSprites, shapePuzzleSprites;
13
14 private List<Sprite> gamePuzzles = new List<Sprite>();
15
16 private List<Button> puzzleButtons = new List<Button>();
17
18 private List<Animator> puzzleButtonAnimators = new List<Animator>();
19
20 private int level;
21 private string selectedPuzzle;
22
23 private int looper;
24
25
26 void Awake()
27 {
28 candyPuzzleSprites = Resources.LoadAll<Sprite>("Art/Candies/Candy");
29 transportPuzzleSprites = Resources.LoadAll<Sprite>("Art/Transport/Transport");
30 shapePuzzleSprites = Resources.LoadAll<Sprite>("Art/Shapes/Shapes");
31
32 }
33
34 void PrepareGameSprites()
35 {
36 gamePuzzles.Clear();
37 gamePuzzles = new List<Sprite> ();
38
39 int index = 0;
40
41 switch (level)
42 {
43 case 0:
44 looper = 6;
45 break;
46
47 case 1:
48 looper = 12;
49 break;
50
51 case 2:
52 looper = 18;
53 break;
54
55 case 3:
56 looper = 24;
57 break;
58
59 case 4:
60 looper = 30;
61 break;
62 }
63
64 switch (selectedPuzzle)
65 {
66 case "Candy Puzzle":
67
68 for(int i= 0; i < looper; i++)
69 {
70 if(index == (looper / 2))
71 {
72 index = 0;
73 }
74
75 gamePuzzles.Add(candyPuzzleSprites[index]);
76
77 index++;
78 }
79 break;
80
81 case "Transport Puzzle":
82
83 for (int i = 0; i < looper; i++)
84 {
85 if (index == (looper / 2))
86 {
87 index = 0;
88 }
89
90 gamePuzzles.Add(transportPuzzleSprites[index]);
91
92 index++;
93 }
94 break;
95
96 case "Shape Puzzle":
97
98 for (int i = 0; i < looper; i++)
99 {
100 if (index == (looper / 2))
101 {
102 index = 0;
103 }
104
105 gamePuzzles.Add(shapePuzzleSprites[index]);
106
107 index++;
108 }
109 break;
110
111
112 }
113
114 Shuffle(gamePuzzles);
115 }
116
117 void Shuffle(List<Sprite> list)
118 {
119 for (int i = 0; i < list.Count; i++)
120 {
121 Sprite temp = list[i];
122 int randomIndex = Random.Range(i, list.Count);
123 list[i] = list[randomIndex];
124 list[randomIndex] = temp;
125
126 }
127 }
128
129 public void SetLevelAndPuzzle(int level, string selectedPuzzle)
130 {
131 this.level = level;
132 this.selectedPuzzle = selectedPuzzle;
133
134 PrepareGameSprites();
135
136 puzzleGameManager.SetGamePuzzleSprites(this.gamePuzzles);
137 }
138
139 public void SetPuzzleButtonsAndAnimators(List<Button> puzzleButtons, List<Animator> puzzleButtonAnimators)
140 {
141 this.puzzleButtons = puzzleButtons;
142 this.puzzleButtonAnimators = puzzleButtonAnimators;
143
144 puzzleGameManager.SetUpButtonsAndAnimators(puzzleButtons, puzzleButtonAnimators);
145 }
146 }